#include <gtest/gtest.h>
#include <thread>
#include "../vas_utils.h"

using namespace vas;

void Log() {
    std::stringstream msg;
    msg << "thread " << std::this_thread::get_id() << " is logging ";
    for (int i = 0; i < 100; i++) {
        VAS_INFO(msg.str());
    }
}

TEST(UitlsTest, LogAndBanner) {
    ASSERT_NO_THROW(VAS_INFO(Banner()));
    ASSERT_NO_THROW(VAS_ERROR("test the error log"));
    std::thread t1(Log);
    std::thread t2(Log);
    std::thread t3(Log);
    std::thread t4(Log);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
}

TEST(UitlsTest, RandomInt) {
    ASSERT_THROW(RandomInt(2, 1), std::runtime_error);
    ASSERT_NO_THROW(RandomInt(1, 2));
    ASSERT_NO_THROW(RandomInt(1, 2));
    ASSERT_EQ(RandomInt(1, 1), 1);
    ASSERT_GE(RandomInt(1, 2), 1);
    ASSERT_LE(RandomInt(1, 2), 2);
}

TEST(UitlsTest, GetCurrentTime) {
    ASSERT_NO_THROW(GetCurrentTime());
    ASSERT_EQ(time(nullptr), GetCurrentTime() / 1000);
}

TEST(UitlsTest, GetFormatCurrentTime) {
    ASSERT_NO_THROW(GetFormatCurrentTime());
    ASSERT_GT(GetFormatCurrentTime().size(), 0);
}

TEST(UitlsTest, FileToString) {
    ASSERT_NO_THROW(FileToString("conf.json"));
    std::string str = FileToString("conf.json");
    ASSERT_GT(str.size(), 0);
}

TEST(UitlsTest, StringToJson) {
    std::string json_str = "{\n"
                           "    \"channel\": \"channel_001\", \n"
                           "    \"timestamp\": 1599639857, \n"
                           "    \"coordinate\": [0, 0, 1280, 720] \n "
                           "}";
    Json::Value root = StringToJson(json_str);
    ASSERT_EQ(root["channel"], "channel_001");
    ASSERT_EQ(root["timestamp"], 1599639857);
    ASSERT_EQ(root["coordinate"].size(), 4);
    ASSERT_EQ(root["coordinate"][0], 0);
    ASSERT_EQ(root["coordinate"][1], 0);
    ASSERT_EQ(root["coordinate"][2], 1280);
    ASSERT_EQ(root["coordinate"][3], 720);
}

TEST(UitlsTest, MatAndBase64) {
    cv::Mat img(300, 200, CV_32FC3, cv::Scalar(0, 0, 255));
    std::string str = MatToBase64(img, "bmp");
    cv::Mat img2 = Base64ToMat(str);
    ASSERT_EQ(img.cols, img2.cols);
    ASSERT_EQ(img.rows, img2.rows);
    ASSERT_EQ(img.channels(), img2.channels());
}